package org.infodavid.common.services;

import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IDocumentCategoryListItem;
import org.infodavid.common.impl.model.criteria.CategoryCriteriaImpl;
import org.infodavid.common.model.AbstractBeanTest;
import org.infodavid.common.model.DocumentCategoryTest;
import org.infodavid.common.model.IDocumentCategory;
import org.infodavid.common.model.criteria.ICategoryCriteria;
import org.infodavid.common.persistence.DocumentCategoryDataService;
import org.infodavid.common.services.DocumentService;

/**
 * The Class DocumentCategoryServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class DocumentCategoryServiceTest
    extends
    AbstractGenericServiceTest<IDocumentCategoryListItem,IDocumentCategory,ICategoryCriteria,DocumentCategoryDataService> {

	final private static Map<ECrudMethod,String> METHODS;

	static {
		METHODS = new HashMap<ECrudMethod,String>();

		METHODS.put(ECrudMethod.ADD, "addCategory");
		METHODS.put(ECrudMethod.DELETE, "removeCategory");
		METHODS.put(ECrudMethod.LIST, "findCategories");
		METHODS.put(ECrudMethod.RETRIEVE, "getCategory");
		METHODS.put(ECrudMethod.UPDATE, "updateCategory");
	}

	/** The delegate. */
	private final DocumentCategoryTest delegate = new DocumentCategoryTest(this);

	/**
	 * The Constructor.
	 */
	public DocumentCategoryServiceTest() {
		super(DocumentService.getInstance(), METHODS);
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @return the data service class
	 * @see org.infodavid.common.services.AbstractServiceTest#getDataServiceClass()
	 */
	@Override
	public Class<DocumentCategoryDataService> getDataServiceClass() {
		return DocumentCategoryDataService.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractServiceTest#buildCriteria()
	 */
	@Override
	public ICategoryCriteria buildCriteria() {
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setNamePattern("name");

		return criteria;
	}

	/**
	 * Test find by name.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByName() throws Exception {
		final IDocumentCategory value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findCategoryByName(getContextMock(), value.getName());
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNull("An exception occurs", exception);
		Assert.assertNotNull("Data is null", value);
		Assert.assertNotNull("Key is null", value.getKey());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameWithoutValue() throws Exception {
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findCategoryByName(getContextMock(), null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameUsingAnInvalidContext() throws Exception {
		final IDocumentCategory value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findCategoryByName(null, value.getName());
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractGenericServiceTest#testCreate()
	 */
	@Override
	public void testCreate() throws Throwable {}

	/**
	 * Test add.
	 * @throws Exception the exception
	 */
	@Test
	public final void testAddWithoutName() throws Exception {
		final IDocumentCategory value = create(1).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		value.setName(null);

		try {
			((DocumentService)getService()).addCategory(getContextMock(), value);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.persistence.AbstractDataServiceTest#getBeanTest()
	 */
	@Override
	protected AbstractBeanTest<IDocumentCategory> getBeanTest() {
		return delegate;
	}
}
